package com.erp.shop.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.erp.shop.mapper.ErpSellerStoreEmpowerMapper;
import com.erp.shop.service.IErpSellerStoreEmpowerService;
import com.erp.shop.service.IErpSellerStoreFreightTemplateService;
import com.ruoyi.common.core.annotation.RepeatSubmit;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;

import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;

import com.ruoyi.shop.api.domain.ErpSellerStoreEmpower;
import com.ruoyi.shop.api.domain.ErpSellerStoreEmpowerExtend;
import com.ruoyi.shop.api.domain.ErpSellerStoreFreightTemplate;
import com.ruoyi.shop.api.domain.bo.ErpSellerStoreFreightTemplateBo;
import com.ruoyi.shop.api.domain.vo.ErpSellerStoreFreightTemplateVo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * 店铺运费模板
 *
 * @author erp
 * @date 2023-03-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/seller/sellerStoreFreightTemplate")
public class ErpSellerStoreFreightTemplateController extends BaseController {

	private final IErpSellerStoreFreightTemplateService iErpSellerStoreFreightTemplateService;
	private final IErpSellerStoreEmpowerService iErpSellerStoreEmpowerService;
	private final ErpSellerStoreEmpowerMapper erpSellerStoreEmpowerMapper;

	@GetMapping("/selectList")
	public R<Object> selectList(ErpSellerStoreFreightTemplateBo bo) {
		List<ErpSellerStoreFreightTemplateVo> list = iErpSellerStoreFreightTemplateService.queryList(bo);
		List<Map<String, Object>> resList = new ArrayList<>();
		list.forEach(vo -> {
			Map<String, Object> map = new HashMap<>();
			map.put("lable", vo.getTemplateName());
			map.put("value", vo.getTemplateId());
			resList.add(map);
		});

		return R.ok(resList);
	}

	@GetMapping("/getFreightTemplateOneByStoreIdAndName/{storeId}/{mbName}")
	R<Object> getFreightTemplateOneByStoreIdAndName(@PathVariable("storeId") Long storeId,@PathVariable("mbName")  String mbName){
		return iErpSellerStoreFreightTemplateService.getFreightTemplateOneByStoreIdAndName(storeId,mbName);
	}

	@GetMapping("/getFreightTemplateLogisticsCompany/{templateId}/{storeId}")
	R<Object> getFreightTemplateLogisticsCompany(@PathVariable("templateId") Long templateId,@PathVariable("storeId")  Long storeId){
		return iErpSellerStoreFreightTemplateService.getFreightTemplateLogisticsCompany(templateId,storeId.toString());
	}

	@GetMapping("/getSellerStoreFreightTemplate/{storeId}")
	R<Object> getSellerStoreFreightTemplate(@PathVariable("storeId") Long storeId){
		return iErpSellerStoreFreightTemplateService.getSellerStoreFreightTemplate(storeId);
	}

	/**
	 * 批量获取店铺运费模板
	 */
	@GetMapping("/getSellerStoreFreightTemplateAll")
	public R<String> getSellerStoreFreightTemplateAll() {
		List<ErpSellerStoreEmpower> storeList = iErpSellerStoreEmpowerService.getEmpowerStore();
		for (ErpSellerStoreEmpower store : storeList) {
			try {
				iErpSellerStoreFreightTemplateService.getSellerStoreFreightTemplate(store.getId());
			} catch (Exception e) {
				continue;
			}
		}
		return R.ok();
	}

	/**
	 * 批量获取店铺运费模板
	 */
	@GetMapping("/getFreightTemplateByStoreUserName/{storeUserName}")
	public R<Object> getFreightTemplateByStoreUserName(@PathVariable("storeUserName") String storeUserName) {
		ErpSellerStoreEmpower storeEmpower = erpSellerStoreEmpowerMapper.selectStoreByStoreUserName(storeUserName);

		return	iErpSellerStoreFreightTemplateService.getSellerStoreFreightTemplate(storeEmpower.getId());
	}

	/**
	 * 查询店铺运费模板列表
	 */
	@SaCheckPermission("seller:sellerStoreFreightTemplate:list")
	@GetMapping("/list")
	public TableDataInfo<ErpSellerStoreFreightTemplateVo> list(ErpSellerStoreFreightTemplateBo bo,
			PageQuery pageQuery) {
		return iErpSellerStoreFreightTemplateService.queryPageList(bo, pageQuery);
	}

	/**
	 * 获取店铺运费模板详细信息
	 *
	 * @param id 主键
	 */
	@SaCheckPermission("seller:sellerStoreFreightTemplate:query")
	@GetMapping("/{id}")
	public R<ErpSellerStoreFreightTemplateVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
		return R.ok(iErpSellerStoreFreightTemplateService.queryById(id));
	}

	/**
	 * 新增店铺运费模板
	 */
	@SaCheckPermission("seller:sellerStoreFreightTemplate:add")
	@Log(title = "店铺运费模板", businessType = BusinessType.INSERT)
	@RepeatSubmit()
	@PostMapping()
	public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpSellerStoreFreightTemplateBo bo) {
		return toAjax(iErpSellerStoreFreightTemplateService.insertByBo(bo));
	}

	/**
	 * 修改店铺运费模板
	 */
	@SaCheckPermission("seller:sellerStoreFreightTemplate:edit")
	@Log(title = "店铺运费模板", businessType = BusinessType.UPDATE)
	@RepeatSubmit()
	@PutMapping()
	public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpSellerStoreFreightTemplateBo bo) {
		return toAjax(iErpSellerStoreFreightTemplateService.updateByBo(bo));
	}

	/**
	 * 删除店铺运费模板
	 *
	 * @param ids 主键串
	 */
	@SaCheckPermission("seller:sellerStoreFreightTemplate:remove")
	@Log(title = "店铺运费模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
		return toAjax(iErpSellerStoreFreightTemplateService.deleteWithValidByIds(Arrays.asList(ids), true));
	}
}
